/*jslint newcap:true, browser: true, devel: true, bitwise: true, white: true, plusplus: true */
/*global $:true, define:true */

/**
 * @module Core
 */

define(['WID/Core/Class','WID/Core/Engine','WID/Core/AJAX','WID/Core/SubViews','WID/Core/WIDLoadingScreen'],
function(Class,Engine,AJAX,SubViews,WIDLoadingScreen) {
"use strict";

/** 
 * @class View
 * @constructor
 */
var View = function(name, parent, controllerName, subViewsNames) {
	this.Name = name;
	this.Parent = parent;
	this.MainElement = View.createMainElement(name,parent);
	this.Translation = new Translation(Engine.APP.languagePrefix,name);
	
	if (controllerName) {
		this.ControllerPath = 'Controllers/'+controllerName+'.php';
	} else {
		this.ControllerPath = this.Parent.ControllerPath;
	}
	
	if (subViewsNames) {
		this.Subs = new SubViews(this,subViewsNames);
		this.SubsPath = name+'/';
	}
};

View.createMainElement = function(name, parent) {
	var main = document.createElement('div');
		main.id = ((parent)?parent.Name+'V'+name:name)+'V';
	return main;
};

return Class(View,{methods: {
	constructor: View,
	
	LoadingScreen: new WIDLoadingScreen(),
	
	showLoading: function() {
		this.LoadingScreen.show(this.MainElement);
	},
	
	hideLoading: function() {
		this.LoadingScreen.hide(this);
	},
	
	init: function() {
		this.showLoading();
		var context = this;
		this.loadTranslation(function(translation) {
			//context.initActions(context.actions);
			//context.loadData();
			context.render(translation);
			if (context.Subs) {
				context.Subs.load();
			}
		});
	},
	
	
	loadTranslation: function(onLoad) {
		window.setTimeout(onLoad,1000);
		//onLoad();
		//AJAX.get(this.LanguagePrefix+this.Name,onLoad);
	},
	
	initActions: function() {
		
	},
	
	addAction: function() {
		
	},
	
	execAction: function(name) {
		
	},
	
	sendAction: function(name,params,callback) {
		params = params||{};
		params.action = name;
		
		AJAX.post(this.ControllerPath,params,callback);
	},
	
	render: function(translation) {
		throw new Error('[VIEW] undefined method render');
	},
	
	onRender: function() {
		
		if (!this.Parent) {
			$('#SimpleApp').append(this.MainElement);
		} else {
			this.Parent.MainElement.appendChild(this.MainElement);
		}
		this.hideLoading();
	},
	
	loadData: function() {},
	
	onSubsLoaded: function() {
		
	},
	 
	dispose: function(onDone) {
		this.Subs.dispose(onDone);
	}
}});

});
